package main

import (
	"github.com/hajimehoshi/ebiten/v2"
	"time"
)

type Input struct {
	lastBulletTime time.Time
}

func (i *Input) Update(ship *Ship, cfg *Config, g *Game) {

	if ebiten.IsKeyPressed(ebiten.KeyLeft) {
		ship.X -= cfg.ShipSpeed
		if ship.X < 0 {
			ship.X = 0
		}
	} else if ebiten.IsKeyPressed(ebiten.KeyRight) {
		ship.X += cfg.ShipSpeed
		if ship.X > float64(cfg.ScreenWidth-ship.Width) {
			ship.X = float64(cfg.ScreenWidth - ship.Width)
		}
	}
	if ebiten.IsKeyPressed(ebiten.KeySpace) {
		if cfg.BulletInterval < time.Now().Sub(i.lastBulletTime).Milliseconds() {
			bullet, _ := NewBullect(cfg, ship)
			g.addBullet(bullet)
			i.lastBulletTime = time.Now()
		}
	}
}
